package com.sn.author.common.sdk.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sn.author.common.sdk.AbstractResponse;
import com.sn.author.common.sdk.Client;
import com.sn.author.common.sdk.request.Request;
import com.sn.author.common.sdk.util.HttpRequest;
import com.sn.author.common.sdk.util.ParserFactory;
import com.sn.author.common.sdk.util.Utils;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
@Slf4j
public class YKClient implements Client {
    public static final String CHARSET_UTF8 = "UTF-8";
    private int connectTimeout = 5000;
    private int readTimeout = 2000;
    private String content;

    public YKClient() {
    }

    @Override
    public <T extends AbstractResponse> T execute(Request<T> request) throws Exception {
        String apiUrl = request.getApiUrl();
        log.info("apiUrl:{}", apiUrl);
        if (Utils.isEmpty(apiUrl)) {
            throw new Exception("Url must not be null");
        }
        String responseMsg = "";
        try {
            switch (request.getMethod()) {
                case "POST":
                    responseMsg = HttpRequest.sendPost(apiUrl, (Map) request.getUrlParams());
                    break;
                case "GET":
                    responseMsg = HttpRequest.sendGetRequest(apiUrl);
                    break;
                default:
            }
            log.info("response message json:" + responseMsg);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("【调用api出现异常 url:{" + request.getApiUrl() + "} 报错文本:{" + e.getMessage() + "}】");
        }

        String jsonStr = machineJson(responseMsg, request);

        return this.parser(jsonStr, request.getResponseClass());
    }


    private String machineJson(String responseMsg, Request request) throws Exception {

        JSONObject resJson = JSON.parseObject(responseMsg);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("code", resJson.getIntValue("code"));
        jsonObject.put("success", resJson.getIntValue("code") == 0);
        jsonObject.put("url", request.getApiUrl());
        jsonObject.put("message", resJson.getString("msg"));
        jsonObject.put("resData", resJson);

        //获取泛型类
        Class cls;
        try {
            cls = (Class) ((ParameterizedType) request.getResponseClass().getGenericSuperclass()).getActualTypeArguments()[0];
        } catch (Exception e) {
            throw new Exception(e);
        }
        jsonObject.put("data", JSON.parseObject(JSON.toJSONString(resJson.get("data")), cls));
        return jsonObject.toJSONString();
    }

    private <T extends AbstractResponse> T parser(String responseMsg, Class<T> responseClass) throws Exception {
        return ParserFactory.JSON_PARSER.parse(responseMsg, responseClass);
    }
}
